package com.shou.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.shou.CommonVar;
import com.shou.dao.ActivityDao;
import com.shou.dao.PictureTextMsgDao;
import com.shou.entity.Activity;
import com.shou.entity.PictureTextMsg;
import com.shou.util.DateUtil;
import com.shou.util.FileUtil;
import com.shou.util.ImageUtil;

@Service("activityService")
public class ActivityService {
	
	@Autowired
	PictureTextMsgDao pictureTextMsgDaoImpl;
	
	@Autowired
	ActivityDao activityDaoImpl;
	
	
	public PictureTextMsg getPictureTextMsg(Integer id){
		return pictureTextMsgDaoImpl.get(PictureTextMsg.class, id);
	}
	
	public List<PictureTextMsg> getAllPictureText() {
		List<PictureTextMsg> list = pictureTextMsgDaoImpl.findAll(PictureTextMsg.class);
		return list;
	}
	
	//获取首页的图文
	public PictureTextMsg getPictureText(String cateName) {
		
		//获取首页的图文
		PictureTextMsg text = pictureTextMsgDaoImpl.getBy(PictureTextMsg.class,"type",cateName);
		return text;
	}
	
	public void saveText(Integer id,String content,String description) {
		if(id == null ){ //新增
			PictureTextMsg text = new PictureTextMsg();
			text.setDescription(description);
			text.setMsg(content);
			text.setCreateTime(new Date());
			text.setUpdateTime(new Date());
			pictureTextMsgDaoImpl.saveOrUpdate(text);
		}
		else { //
			PictureTextMsg msg = pictureTextMsgDaoImpl.get(PictureTextMsg.class, id);
			msg.setDescription(description);
			msg.setMsg(content);
			msg.setUpdateTime(new Date());			
			pictureTextMsgDaoImpl.saveOrUpdate(msg);
		}
	}
	
	public Activity getActivity(Integer activityId){
		
		return activityDaoImpl.get(Activity.class, activityId);
	}
	
	public List<Activity> activityList() {
		return activityDaoImpl.getListBy(Activity.class,"status",CommonVar.USE_ONUSE,"id",false);
	}
	
	public void saveActivity(Activity activity) {
		
		if(activity.getId() == null){
			activity.setCreateTime(new Date());
			activity.setUpdateTime(new Date());
			activity.setUseStatus(CommonVar.USE_ONUSE);
			activityDaoImpl.saveOrUpdate(activity);
		}
		else {
			Activity oldAct = activityDaoImpl.get(Activity.class,activity.getId());
			oldAct.setActivityName(activity.getActivityName());
			oldAct.setDescription(activity.getDescription());
			oldAct.setImageUrl(activity.getImageUrl());
			oldAct.setLinkUrl(activity.getLinkUrl());
			oldAct.setUseStatus(activity.getUseStatus());			
			activityDaoImpl.saveOrUpdate(oldAct);
		}
	}
	
	public void deleteActivity(Integer activityId) {
		Activity activity =	activityDaoImpl.get(Activity.class,activityId);
		activity.setUseStatus(CommonVar.USE_NOUSE);
		activityDaoImpl.saveOrUpdate(activity);
	}
	
	public String uploadActivityImage(@RequestParam("cmfile") CommonsMultipartFile cmfile, Integer fsize) {
		
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM"); 
		String path = df.format(new Date())  ;
		
		String folder = "activity";
		String fileName = FileUtil.newFileName(cmfile.getOriginalFilename());
		String filePath = CommonVar.FILE_PREFIX+"/"+folder+"/"+path;
		File file = new File(filePath);
		if(!file.exists()){
			file.mkdir(); //如果不存在则新建目录
		}
		filePath = filePath + "/"+fileName; 		
		ImageUtil.uploadPicture(cmfile, fsize,CommonVar.FILE_PREFIX+"/"+filePath);
		return filePath;
	}
}
